package Map_2;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

public class Market implements Serializable{
    private String name;
    private String adress;
    public static final String OBJFILE="Market.txt";
    //从文件中读取对象
    public static Market getFromFile(){
        try {
            ObjectInputStream o = new ObjectInputStream(new FileInputStream(OBJFILE));
            Market market=(Market)o.readObject();
            o.close();
            return market;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public static void setObjfile(Market a){
        try {
            ObjectOutputStream o2 = new ObjectOutputStream(new FileOutputStream(OBJFILE));
           o2.writeObject(a);
           o2.close();


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Market() {

    }

    public Market(String name, String adress) {
        this.name = name;
        this.adress = adress;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAdress() {
        return adress;
    }

    public void setAdress(String adress) {
        this.adress = adress;
    }


    //会员开卡；
    List<Member> list = new ArrayList<>();

    public boolean add(String name, String ID, String phoneNumber, int integral, String password, String cardtime) {
        for (Member member : list) {
            if (member.getID().equals(ID)) {
                System.out.println("会员号重复，添加失败");
                return false;
            }
        }
        Calendar calendar = Calendar.getInstance();
        String cardtime1 = new SimpleDateFormat("yyyy年MM月dd日 ").format(calendar.getTime());
        list.add(new Member(name, ID, phoneNumber, integral, password, cardtime1));
        return true;

    }
    //显示会员信息；

    public void showMember() {
        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            Object o = iterator.next();
            System.out.println(o);
        }
    }

    //显示商场信息
    public void show() {
        System.out.println("商场姓名：" + getName() + "商场地址：" + getAdress());
    }

    //积分兑换；
    public boolean exchange(String cardNo, int no) {
        for (Member member : list) {
            if (cardNo.equals(member.getID())) {
                System.out.println("您总共的积分为：" + member.getIntegral() + "分");
                if (member.getIntegral() >= 100 && no == 1) {
                    member.setIntegral(member.getIntegral() - 100);
                    System.out.println("兑换餐巾纸一包");
                    System.out.println("剩余积分为" + member.getIntegral());
                    return true;
                }
                if (member.getIntegral() >= 400 && no == 2) {
                    member.setIntegral(member.getIntegral() - 400);
                    System.out.println("兑换剃须刀一把");
                    System.out.println("剩余积分为" + member.getIntegral());
                    return true;
                }
                if (member.getIntegral() >= 1000 && no == 3) {
                    member.setIntegral(member.getIntegral() - 1000);
                    System.out.println("兑换铁锅一个");
                    System.out.println("剩余积分为" + member.getIntegral());
                    return true;
                }
            }
        }
        System.out.println("兑换失败");
        return false;


    }

    //添加会员积分
    public boolean increaseScore(String cardNo, int score) {
        for (Member member : list) {
            if (cardNo.equals(member.getID())) {
                member.setIntegral(member.getIntegral() + score);
                System.out.println("积分添加成功");
                return true;
            }
        }
        System.out.println("未找到会员信息");
        return false;
    }


    //查询积分；
    public boolean check(String cardNo) {
        for (Member member : list) {
            if (cardNo.equals(member.getID())) {
                System.out.println(member.getIntegral() + "分");
                return true;
            }
        }
        System.out.println("卡号输入有误");
        return false;

    }

    //修改密码
    public boolean changePassword(String cardNo, String oldpassword, String newpassword) {
        for (Member member : list) {
            if (cardNo.equals(member.getID()) && oldpassword.equals(member.getPassword())) {
                member.setPassword(newpassword);
                System.out.println("密码修改成功，新密码是" + newpassword);
                return true;
            }
        }
        System.out.println("密码修改失败，请验证后重新输入");
        return false;
    }
}
